Some Apple Scripts for Safari.md (5839B)
1 --- 2 title: Some Apple Scripts for Safari 3 author: Yohanes Bandung Bondowoso 4 description: macOS provides way to change default key bindings for any applications, and changed some of Safari's key bindings with it too. I discovered other way to "change" or add key bindings with Apple Script and FastScripts. It is awesome that I'd like to share it. 5 tags: 6 - tools #tools 7 - apple #apple 8 twitter: 9 link: https://twitter.com/ybbond/status/1297602893504106496 10 slug: safari-apple-script 11 date: 2020-08-23T19:48:48+0700 12 ID: 20200823194848 13 --- 14 15 Safari Technology Preview is my primary web browser when I am not developing website. It is the beta equivalent for Safari that features the latest updates, including the «Tracker Blocker» that will be released to public on macOS Big Sur. 16 17 One of the main losses is the lack of good browser extension to simulate Vim key bindings. [Vimari](https://github.com/televator-apps/vimari) is okay, but it doesn't work correctly on GitHub. The other losses are the lack of accidental quit prevention and alternative key binding to change tab. 18 19 ## Prevent accidental quit 20 21 Firefox and Chrome has option to warn us if we press `⌘ + Q` accidentally. Safari doesn't provide this option. This often happen when I want to close a tab with `⌘ + W`, but my clumsy ring finger misses that `W`. Previously, I use `App Shortcuts` modifier (System Preferences → Keyboard → Shortcuts → App Shortcuts) and assign it to `⌘ + ⌥ + Q`. It works, but I am curious whether there's another way. 22 23 I browsed the internet, and found that **John Gruber** already shared the solution for this issue. You can read it on this post: [Quit Confirmation for Safari on MacOS](https://daringfireball.net/2020/01/quit_confirmation_for_safari_on_macos) via DaringFireball.net. 24 25 For your convenience, I will share the slightly modified script here: 26 27 ```AppleScript 28 tell application "Safari Technology Preview" 29 set windowCount to count windows 30 set tabCount to 0 31 32 repeat with currWindow in every window 33 set tabCount to tabCount + (count tabs of currWindow) 34 end repeat 35 36 -- Make a string like "1 window containing 3 tabs." 37 if windowCount is 1 then 38 set msg to windowCount & " window containing " as string 39 else 40 set msg to windowCount & " windows containing " as string 41 end if 42 if tabCount is 1 then 43 set msg to msg & tabCount & " tab." as string 44 else 45 set msg to msg & tabCount & " tabs." as string 46 end if 47 48 display alert ¬ 49 "Are you sure you want to quit Safari?" message msg ¬ 50 buttons {"Cancel", "Quit"} ¬ 51 giving up after 60 52 if button returned of result is "Quit" then quit 53 end tell 54 ``` 55 56 Notice that you might want to change from `Safari Technology Preview` to only `Safari`. 57 58 You'd want to put the script to `Script Editor.app`, and —as suggested by John, use [FastScripts](https://red-sweater.com/fastscripts/). FastScripts is awesome! Credits to red sweater. What you should do is move the scripts generated by Script Editor to FastScripts' folder for Safari and assign your preferred key bindings for it. Obviously you'd want to assign it to `⌘ + Q`. 59 60 More info on FastScripts, you can check [their FAQ here](https://help.red-sweater.com/fastscripts/faq/). 61 62 Because we've covered the issue about quitting, now moving to the next issue. 63 64 ## Alternative key bindings to change tab 65 66 The default key bindings to change tab for Safari is `⌃ + ⇥` for Next Tab, and `⌃ + ⇧ + ⇥` for Previous Tab. I'd like to add the already familiar key bindings that exist on Firefox and Chrome, that is `⌘ + ⌥ + ←` for Previous Tab and `⌘ + ⌥ + →` for Next Tab. 67 68 I tried using Mac's built in `App Shortcuts` modifier (System Preferences → Keyboard → Shortcuts → App Shortcuts) to modify it, but I'd lose the original key bindings. I want them both to coexist. 69 70 My solution is by using the Apple Script and FastScripts too. 71 72 Hereby the script to change tab to Next Tab: 73 74 ```AppleScript 75 tell application "Safari Technology Preview" 76 tell window 1 77 set myTab to (index of current tab) 78 set goodtab to (myTab + 1) as integer 79 try 80 set current tab to tab goodtab 81 on error 82 set current tab to tab 1 83 end try 84 end tell 85 end tell 86 ``` 87 88 and the script to change tab to Previous Tab: 89 90 ```AppleScript 91 tell application "Safari Technology Preview" 92 tell window 1 93 set myTab to (index of current tab) 94 set goodtab to (myTab - 1) as integer 95 if goodtab = 0 then 96 tell application "Safari Technology Preview" to set lastTab to the number of tabs in window 1 97 set current tab to tab lastTab 98 else 99 set current tab to tab goodtab 100 end if 101 end tell 102 end tell 103 ``` 104 105 You'd want to change the `Safari Technology Preview` too. The scripts are straight forward, one of the advantages of Apple Script's strange syntax. Let me explain a bit about the `try ... on error` and `if ... else` part. 106 107 When you use the Next Tab script and reach the last tab, you'd encounter error because the last tab + 1 is nowhere to be found. In that case, we move to the first tab. That's the expected behavior too, because we'd want the key binding to keep looping on the available tabs. 108 109 For the Previous Tab script, it gets a little complicated, because there is no property or method to get the length of available tabs that I know of. So I ask (`tell`) Safari to define a `lastTab` variable with the number of available tabs on the active Safari window as its value. 110 111 Prior to this day, I never thought that I'd use Apple Scripts and these kind of macOS utility tools helps my day this much. I read Mac Power User, listen to Rosemary Orchard's podcasts [about productivity](https://nestedfolderspodcast.com) and [automating some tasks](https://www.relay.fm/automators), and others. Now I know how awesome the previously overlooked tools is.